package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.PraiserInsertDao;
import com.qf.lanzhou.blog.api.pojo.entity.Praise;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author jian
 * @Date 2020/12/17 15:00
 * @JDK 1.8
 * @Version 1.0
 */
public class PraiserInsertDaoimpl implements PraiserInsertDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());


    @Override
    public List<Praise> select(String praiseStatus) {
        String sql = "SELECT * from t_praise where praise_status=1";
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        try {
            return queryRunner.query(sql, new BeanListHandler<Praise>(Praise.class, processor));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int insert(String userId, String articleId, String praiseStatus) {
        String sql = "INSERT into t_praise (user_id,article_id,praise_status) VALUES (?,?,?)";
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        Object[] params = {userId, articleId, praiseStatus};
        try {
            return queryRunner.update(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(String userId, String articleId) {
        String sql = "UPDATE t_praise SET praise_status=1 WHERE user_id=? AND article_id=?;";
        Object[] params = {userId, articleId};
        int update=0;
        try {
            update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }


}
